package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysDictType;
import com.cjb.cloudsystem.mapper.SysDictTypeMapper;
import com.cjb.cloudsystem.service.SysDictTypeService;
import com.cjb.cloudsystem.vo.SysDictTypeVO;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SysDictTypesServiceImpl
 * @Date 2023-07-27 21:41
 */
@Service
@AllArgsConstructor
public class SysDictTypeRepositoryImpl extends BaseRepositoryImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(SysDictTypeRepositoryImpl.class);
    /*
     * 注入mapper层
     */
    private final SysDictTypeMapper sysDictTypeMapper;

    /**
     * 单体保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysDictType sysDictType) {
        baseMapper.insert(sysDictType);
        return true;
    }

    /**
     * 批量保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Integer listSave(List<SysDictType> sysDictType) {
        return sysDictTypeMapper.insertBatchSomeColumn(sysDictType);
    }

    /**
     * 批量更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listUpdate(List<SysDictType> sysDictType) {
        sysDictTypeMapper.insertBatchSomeColumn(sysDictType);
    }

    /**
     * 批量删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listDelete(List<SysDictType> sysDictType) {
        List<Long> collect = sysDictType.stream().map(SysDictType::getId).distinct().collect(Collectors.toList());
        sysDictTypeMapper.deleteBatchIds(collect);
    }

    /**
     * 通过id删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public int deleteById(SysDictType sysDictType) {
        return sysDictTypeMapper.deleteById(sysDictType.getId());
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public SysDictType queryById(SysDictType sysDictType) {
        return sysDictTypeMapper.selectById(sysDictType.getId());
    }

    /**
     * 分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page sysDictTypePage, SysDictTypeVO sysDictType) {
          return sysDictTypeMapper.selectPage(sysDictTypePage, getWrapper(sysDictType));
    }

    /**
     * 查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysDictType> getWrapper(SysDictTypeVO query) {
        LambdaQueryWrapper<SysDictType> wrapper = Wrappers.lambdaQuery();
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getId, query.getId());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getDictType, query.getDictType());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getDictName, query.getDictName());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getDictSource, query.getDictSource());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getDictSql, query.getDictSql());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getRemark, query.getRemark());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getSort, query.getSort());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getTenantId, query.getTenantId());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getVersion, query.getVersion());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getDeleted, query.getDeleted());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getCreator, query.getCreator());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getCreateTime, query.getCreateTime());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getUpdater, query.getUpdater());
        }
        if (Common.isNotNull(query.getId())) {
            wrapper.eq(SysDictType::getUpdateTime, query.getUpdateTime());
        }
        wrapper.orderByDesc(SysDictType::getId);
        return wrapper;
    }


    /**
     * 返回全部信息
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysDictType> returnAllData() {
        return sysDictTypeMapper.selectList(null);
    }

}